const http = require('../../../../utils/http.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: http.imgUrl,
    navBar: {
      title: '设置个人头像',
      btn: 'goBack'
    },
    avatarImg:''
  },

  /**
   * 返回
   */
  goBack() {
    http.goBack()
  },

  /**
   * 获取用户头像
   */
  getAvatar(token) {
    let that = this
    let url = 'Ucenter_getAvatar'

    http._post(
      url,
      { token: token },
      res => {
        // console.log(res.data)
        let data = res.data
        if (data.re === '1') {
          that.setData({
            avatarImg: data.data
          })
        } else {
          wx.showToast({
            title: data.info,
            icon: 'none',
            duration: 3000
          })
        }
      },
      err => {
        console.log(err)
      }
    )
  },

  /**
   * 从相册选择图片上传
   */
  albumImg() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'] ,
      success: function (res) {
        // console.log(res)
        // that.uploadImg(res)
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        that.setData({
          avatarImg: tempFilePaths
        })

      },
      fail: function () {
        wx.showToast({
          title: '图片选择失败，请稍后再试',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  /**
   * 调用相机拍照上传
   */
  cameraImg() {
    var that = this
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'] ,
      success: function (res) {
        // console.log(res)
        that.uploadImg(res)
      },
      fail: function () {
        wx.showToast({
          title: '图片拍摄失败，请稍后再试',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  /**
   * 上传图片
   */
  uploadImg(file) {
    let that = this
    let token = wx.getStorageSync('token')
    let url = 'Ucenter_uploadImgs'
    let formData = { token: token }

    const uploadTask = wx.uploadFile({
      url: http.http + '/' + url,
      filePath: file.tempFilePaths[0],
      name: 'file',
      herder: { 'content-type': 'multipart/form-data;' },
      formData: formData,
      success: (res) => {
        // console.log(res.data)
        // console.log(JSON.parse(res.data))
        let data = JSON.parse(res.data)
        if (data.re === '1') {
          that.setData({
            avatarImg: data.data.img
          })

          let url_1 = 'Ucenter_changeProfile'
          http._post(
            url_1,
            { token: token, head_img: data.data.img },
            res => {
              // console.log(res.data)
              let data = res.data
              wx.showToast({
                title: data.info,
                icon: 'none',
                duration: 3000
              })
              if (data.re === '1') {
                setTimeout(function () {
                  that.goBack()
                }, 3000)
              }
            },
            err => {
              console.log(err)
            }
          )
        } else {
          wx.showToast({
            title: '上传图片失败，请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const token = wx.getStorageSync('token')
    this.getAvatar(token)  // 获取用户头像
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})